package com.example.himalaya.view_component;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.himalaya.R;
import com.squareup.picasso.Picasso;

import de.hdodenhof.circleimageview.CircleImageView;

public class PlayMusicView extends FrameLayout {

    private Context mContext;

    // UI object
    private View mView;
    private CircleImageView mMusicCover;
    private ImageView mPlayIcon;

    // Animation object
    private Animation mPlayMusicAnim, mPlayNeedleAnim, mStopNeedleAnim;
    private ImageView mNeedle;


    public PlayMusicView(@NonNull Context context) {
        this(context, null);

    }

    public PlayMusicView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PlayMusicView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public PlayMusicView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);


        init(context);
        initView();
        initListener();
        initAnimation();
    }

    private void initListener() {
        mPlayIcon.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }

    private void initAnimation() {
        // 光盘转动的动画
        mPlayMusicAnim = AnimationUtils.loadAnimation(mContext, R.anim.play_music_animation);

        // 指针指向光盘的动画
        mPlayNeedleAnim = AnimationUtils.loadAnimation(mContext, R.anim.play_needle_anim);

        // 指针离开光盘的动画
        mStopNeedleAnim = AnimationUtils.loadAnimation(mContext, R.anim.stop_needle_anim);
    }


    private void init(Context context) {
        mContext = context;

        mView = LayoutInflater.from(mContext).inflate(R.layout.play_music, this, false);

        addView(mView);
    }


    private void initView() {
        mMusicCover = mView.findViewById(R.id.iv_icon);

        mPlayIcon = mView.findViewById(R.id.play_icon);

        mNeedle = findViewById(R.id.needle);
    }


    public void StartPlay() {
        mPlayIcon.setVisibility(GONE);
        mMusicCover.startAnimation(mPlayMusicAnim);
        mNeedle.startAnimation(mPlayNeedleAnim);
    }

    public void StopPlay() {
        mPlayIcon.setVisibility(VISIBLE);
        mMusicCover.clearAnimation();
        mNeedle.startAnimation(mStopNeedleAnim);
    }


    public void setMusicIcon(String cover) {

        if (TextUtils.isEmpty(cover)) {
            mMusicCover.setImageResource(R.mipmap.default_picture);

            return;
        }

        Picasso.with(mContext).load(cover).into(mMusicCover);
    }

}
